package pl.edu.agh.mobsys.activities;

import pl.edu.agh.mobsys.R;
import pl.edu.agh.mobsys.data.ConnectionConfig;
import pl.edu.agh.mobsys.utils.Constants;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Widok z konfiguracją dostępu do serwera XMPP.
 * */
public class ServerConfig extends Activity {

	private EditText server, port, username, password, resource, ip;

	private SharedPreferences prefs;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.server_config);

		ip = (EditText) findViewById(R.id.server_ip);
		server = (EditText) findViewById(R.id.server_hostname);
		port = (EditText) findViewById(R.id.server_port);
		username = (EditText) findViewById(R.id.server_username);
		password = (EditText) findViewById(R.id.server_password);
		resource = (EditText) findViewById(R.id.server_resource);

		prefs = getSharedPreferences(Constants.SHARED_PREFS_KEY_SERVER_CONFIG,
				Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

		ConnectionConfig cc = ConnectionConfig.readFromSharedPreferences(prefs);
		ip.setText(cc.ip);
		server.setText(cc.host);
		port.setText(cc.port);
		username.setText(cc.username);
		password.setText(cc.password);
		resource.setText(cc.resource);

		// guzik z zapisem
		((Button) findViewById(R.id.server_edit_save))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						// walidacja
						final String warn = validate();
						if (warn != null) {
							runOnUiThread(new Runnable() {
								public void run() {
									// wyświetlenie ostrzeżeń z walidacji
									Toast.makeText(getApplicationContext(),
											warn, Toast.LENGTH_SHORT).show();
								}
							});
							// dalej nie robimy
							return;
						}

						ConnectionConfig cc = new ConnectionConfig();
						cc.ip = ip.getText().toString();
						cc.host = server.getText().toString();
						cc.port = port.getText().toString();
						cc.username = username.getText().toString();
						cc.password = password.getText().toString();
						cc.resource = resource.getText().toString();
						
						// utrwalenie ustawień w preferencjach
						cc.persistInsideSharedPreferences(prefs);
						startActivity(new Intent(Constants.CONTACTS_LIST));
					}

					private String validate() {
						
						// pola muszą być wypełnione
						for (EditText et : new EditText[] { server, port,
								username, password }) {
							et.setBackgroundColor(Color.WHITE);
							String tmp = et.getText().toString();
							if (null == tmp || "".equals(tmp)) {
								
								// podświetlenie tego czego brakuje
								et.setBackgroundColor(Color.RED);
								return "Fill in required fields.";
							}
						}

						String tmp = username.getText().toString();
						if (tmp.contains("@")) {
							return "Invalid symbol \"@\" used.";
						}

						return null;
					}
				});

		// powrót do listy kontaktów bez zapisu
		((Button) findViewById(R.id.server_edit_cancel))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						startActivity(new Intent(Constants.CONTACTS_LIST));
					}
				});
	}
}